from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

""" 执行
1. python manage.py makemigrations
2. python manage.py migrate
"""


# Create your models here.

class Admin(models.Model):
    """管理员"""
    username = models.CharField(verbose_name="用户名", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=64)

    def __str__(self):
        return self.username


class Department(models.Model):
    """部门表"""
    rolename = models.CharField(verbose_name="职位名称", max_length=32)

    def __str__(self):
        return self.rolename

    """"""


class user(models.Model):
    """员工表"""
    name = models.CharField(verbose_name="姓名", max_length=32)
    pwd = models.CharField(verbose_name="密码", max_length=64)
    age = models.IntegerField(verbose_name='年龄', validators=[MinValueValidator(18), MaxValueValidator(100)])
    account = models.DecimalField(verbose_name='余额', decimal_places=2, max_digits=10, default=0)
    create_time = models.DateTimeField(verbose_name='入职时间', )
    # 无约束
    # depart_id =models.BigIntegerField(verbose_name='部门ID')

    # 有约束
    # - to 与那张表关联
    # - to_field,表中的那一列关联
    # 2.django 自动
    #   -写的depart
    #   - 生成数据列 depart_id
    # 3.部门表被删除
    #       3.1.级联删除
    # depart = models.ForeignKey(to=Department,to_field="id",on_delete=models.CASCADE())
    depart = models.ForeignKey(verbose_name="所属部门", to=Department, to_field="id", on_delete=models.CASCADE)

    #       3.2.置空
    # depart = models.ForeignKey(to=Department,to_field="id",null=True,blank=null,on_delete=models.SET_NULL())

    # zai django 中做的约束
    GENDER_CHOICES = [
        (1, "男"),
        (2, "女")
    ]
    gender = models.IntegerField(verbose_name="性别", choices=GENDER_CHOICES)

    # LEVEL_CHOICES = [
    #     (1, "1"),
    #     (2, "2"),
    #     (3, "3"),
    #     (4, "4"),
    #     (5, "5")
    # ]
    # level = models.IntegerField(verbose_name="级别", choices=LEVEL_CHOICES)

    def __str__(self):
        return self.name


class UserInfo(models.Model):
    # name = models.CharField(verbose_name="姓名", max_length=32)
    phone = models.CharField(verbose_name="手机号码", max_length=11)
    pwd = models.CharField(verbose_name="密码", max_length=128)


class PrettyNum(models.Model):
    """ 靓号表 """
    mobile = models.CharField(verbose_name="手机号", max_length=11)  # 属性 = 字符串类型（表头名，型最大长度11）
    # 想要允许为空 null=True, blank=True
    price = models.IntegerField(verbose_name="价格", default=0)  # 属性 = int类型（表头名，默认为0）

    # 在django中做的约束
    level_choices = (
        (1, "1级"),
        (2, "2级"),
        (3, "3级"),
        (4, "4级"),
        (5, "5级"),
    )

    level = models.SmallIntegerField(verbose_name="级别", choices=level_choices,
                                     default=1)  # 属性 = 小int类型（表头名，关联级别，默认为1级）

    # 在django中做的约束
    status_choices = (
        (1, "已占用"),
        (2, "未使用")
    )
    status = models.SmallIntegerField(verbose_name="状态", choices=status_choices,
                                      default=2)  # 属性 = 小int类型（表头名，关联级别，默认为未使用）


class Task(models.Model):
    """任务"""
    level_choices = [
        (1, "紧急"),
        (2, '重要'),
        (3, '临时'),
    ]

    level = models.SmallIntegerField(verbose_name="级别", choices=level_choices, default=3)
    title = models.CharField(verbose_name='标题', max_length=64)
    create_time = models.DateTimeField(verbose_name='时间')
    detail = models.TextField(verbose_name='详细信息')
    user = models.ForeignKey(verbose_name='负责人', to=Admin, to_field="id", on_delete=models.CASCADE)

    def to_dict(self):
        # 返回一个包含模型数据的字典
        return {
            'id': self.id,
            'level': self.level,
            'title': self.title,
            'create_time': self.create_time.isoformat(),
            'detail': self.detail,
            'user': self.user.username  # 将 datetime 转换为 ISO 8601 格式的字符串
        }


class Order(models.Model):
    """工单"""
    oId = models.CharField(verbose_name='订单号', max_length=64)
    title = models.CharField(verbose_name='商品名称', max_length=32)
    price = models.IntegerField(verbose_name="价格", default=0)
    status_choices = (
        (1, "已支付"),
        (2, "待支付")
    )
    status = models.SmallIntegerField(verbose_name="状态", choices=status_choices,
                                      default=1)  # 属性 = 小int类型（表头名，关联级别，默认为未使用）
    admin = models.ForeignKey(verbose_name='管理员', to=Admin, to_field="id", on_delete=models.CASCADE)

    # def to_dict(self):
    #     # 返回一个包含模型数据的字典
    #     return {
    #         'id': self.id,
    #         'oId': self.oId,
    #         'title': self.title,
    #         'price': self.price,
    #         'status': self.status,
    #         'admin': self.admin.username  # 将 datetime 转换为 ISO 8601 格式的字符串
    #     }


# class File(models.Model):
#     file = models.FileField(upload_to='documents/')
#     content = models.TextField()  # 用于存储文件内容


class File(models.Model):
    """文件"""
    name = models.CharField(verbose_name='姓名', max_length=32)
    age = models.IntegerField(verbose_name='年龄')
    img = models.CharField(verbose_name='头像', max_length=64)


class FileModal(models.Model):
    """文件"""
    name = models.CharField(verbose_name='城市', max_length=32)
    count = models.IntegerField(verbose_name='人口')
    # 本质上数据库也是CharField,自动保存数据
    img = models.FileField(verbose_name='logo', max_length=128,upload_to='logo/')
